<?php

require_once(WSB_MODULES_PATH . 'sherpa/forms/FeuserData.php');

class Sherpa_FeuserDataController extends WSB_Xtension_Extension
{

	/**
	 * @return string
	 */
	public function renderForBackend()
	{
		return get_class($this);
	}

	/**
	 * Initialisierung
	 */
	public function init()
	{
		$this->initTranslate();
	}

	/**
	 * Index Action
	 *
	 * @param Sherpa_Form_FeuserData $form
	 */
	public function indexAction(Sherpa_Form_FeuserData $form = null)
	{
		if(!WSB_Auth_Frontend::getInstance()->hasIdentity()) {
			return;
		}
		if(is_null($form)) {
			$form = new Sherpa_Form_FeuserData();
			$feuser = WSB_Auth_Frontend::getInstance()->getIdentity();
			$form->setDefaults($feuser->toArray());
		}
		$this->assignToView('form', $form);
		$this->renderView('feuserdata/index.phtml');
	}

	/**
	 * Save Action
	 */
	public function saveAction()
	{
		if(!WSB_Auth_Frontend::getInstance()->hasIdentity()) {
			return;
		}
		$request = $this->getRequest();
		$form = new Sherpa_Form_FeuserData();
		//$form->populate($request->getPost());
		if($request->isPost() && $form->isValid($request->getPost())) {
			$feuser = WSB_Auth_Frontend::getInstance()->getIdentity();
			$feuser->firstname = $form->getValue('firstname');
			$feuser->lastname = $form->getValue('lastname');
			$feuser->password = null; // @see: Member_Model_DbTable_Feuser_Row::preSave()
			$feuser->setTable(new Member_Model_DbTable_Feuser());
			$feuser->save();
			$this->assignToView('success', true);
			$this->indexAction();
		} else {
			$this->indexAction($form);
		}
	}

}